<template>
    <div class="adminRole">
        <el-form ref="formRef" :inline="true" :model="form" class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]">
            <el-form-item :label="t('racklocations.locationCode') + ':'" prop="code">
                <el-input v-model="form.code" :placeholder="t('racklocations.enterTheLocationCode')" clearable
                    class="!w-[220px]" />
            </el-form-item>
            <el-form-item :label="t('racklocations.status') + ':'" prop="state">
                <el-select v-model="form.state" clearable :placeholder="t('racklocations.selectionState')"
                    class="!w-[220px]">
                    <!-- <el-option v-for="item in statusSelect" :key="item.value" :label="item.label" :value="item.value" /> -->
                    <el-option :label="t('racklocations.idle')" :value="1" />
                    <el-option :label="t('racklocations.disable')" :value="2" />
                    <el-option :label="t('racklocations.locked')" :value="3" />
                    <el-option :label="t('racklocations.cargoAlreadyLoaded')" :value="4" />
                </el-select>
            </el-form-item>
            <el-form-item :label="t('racklocations.shelf') + ':'" prop="rackid">
                <el-select v-model="form.rackid" clearable :placeholder="t('racklocations.selectShelves')"
                    class="!w-[220px]">
                    <el-option v-for="item in rackselect" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item :label="t('racklocations.correspondingSite') + ':'" prop="stationName">
                <el-input v-model="form.stationName" :placeholder="t('racklocations.enterTheCorrespondingSite')"
                    clearable class="!w-[160px]" />
                <!-- <el-select v-model="form.stationName" clearable placeholder="请选择对应站点" class="!w-[160px]">
          <el-option v-for="item in stationSelect" :key="item.stationid" :label="item.stationname" :value="item.stationid" />
        </el-select> -->
            </el-form-item>
            <el-form-item :label="t('racklocations.locationType') + ':'" prop="attribute">
                <el-select v-model="form.attribute" clearable filterable
                    :placeholder="t('racklocations.selectLocationType')" class="!w-[220px]">
                    <el-option v-for="item in attributeSelect" :key="item.Id" :label="item.Name" :value="item.Code" />
                </el-select>
            </el-form-item>
            <el-form-item :label="t('racklocations.numberOfLayers') + ':'" prop="layer">
                <el-input-number v-model="form.layer" controls-position="right"
                    :placeholder="t('racklocations.inputLayers')" class="!w-[220px]" />
            </el-form-item>
            <el-form-item :label="t('racklocations.column') + ':'" prop="column">
                <el-input-number v-model="form.column" controls-position="right"
                    :placeholder="t('racklocations.enterTheNumberOfColumns')" class="!w-[220px]" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" :icon="useRenderIcon(Search)" :loading="loading" @click="onSearch">
                    {{ t('racklocations.search') }}
                </el-button>
                <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
                    {{ t('racklocations.reset') }}
                </el-button>
            </el-form-item>
        </el-form>

        <PureTableBar :title="t('racklocations.locationManagement')" :columns="newColumns" @refresh="onSearch"
            v-if="tableHtml">
            <template #title>
                <div>
                    <span class="font-bold truncate">
                        {{ t('racklocations.locationManagement') }}
                    </span>
                    <el-dropdown trigger="click" placement="right" :hide-on-click="false"
                        @visible-change="handleCheckboxChange">
                        <el-button type="primary" plain :icon="useRenderIcon(EpArrowDown)" size="small">
                            {{ t('racklocations.filter') }}
                        </el-button>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item v-for="item in simplifiedColumns" :key="item.id">
                                    <el-checkbox v-model="item.isShow" :label="item.label" size="small"
                                        :tabindex="item.id" v-if="item.id !== 66" />
                                </el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                </div>
            </template>
            <template #buttons>
                <el-button type="primary" :icon="useRenderIcon(AddFill)" @click="openDialog()">
                    {{ t('racklocations.addNewStorageLocation') }}
                </el-button>
                <el-button type="primary" :icon="useRenderIcon(signOutBold)" @click="exportExcel()">
                    {{ t('racklocations.export') }}
                </el-button>
                <!-- <el-button type="warning" :icon="useRenderIcon(clearOutlined)" @click="clearrackslocation()">
                     清空出库货位
                </el-button> -->
            </template>
            <template v-slot="{ size, dynamicColumns }">
                <pure-table ref="tableRef" adaptive align-whole="center" row-key="Id" showOverflowTooltip
                    table-layout="auto" :loading="loading" :size="size" :data="dataList" :columns="dynamicColumns"
                    :pagination="pagination" :paginationSmall="size === 'small' ? true : false" :header-cell-style="{
                        background: 'var(--el-fill-color-light)',
                        color: 'var(--el-text-color-primary)'
                    }" @page-size-change="handleSizeChange" @page-current-change="handleCurrentChange">
                    <template #operation="{ row }">
                        <!-- <el-button class="reset-margin" link type="success" :size="size" :icon="useRenderIcon(loadingIcon)"
              @click="initialize(row)">
              初始化
            </el-button> -->
                        <!-- <el-button class="reset-margin" link type="success" :size="size" :icon="useRenderIcon(outboxIcon)" @click="addAppointOut(row)" >
              出库
            </el-button>
            <el-button class="reset-margin" link type="warning" :size="size" :icon="useRenderIcon(outlineDelete)" @click="addDelteLoad(row)">
              删除货载
            </el-button> -->
                        <el-button class="reset-margin" link type="primary" :size="size" :icon="useRenderIcon(EditPen)"
                            @click="openDialog('编辑', row)">
                            {{ t('racklocations.edit') }}
                        </el-button>
                        <el-button class="reset-margin" link type="danger" :size="size" :icon="useRenderIcon(Delete)"
                            @click="delLocation(row)">
                            {{ t('racklocations.delete') }}
                        </el-button>
                        <!-- <el-button class="reset-margin" link :type="row.status == 2 ? 'success' : 'danger'" :size="size"
              :icon="useRenderIcon(gearBold)" @click="handleStatus(row)">
              {{ row.status == 2 ? '启用' : '禁用' }}
            </el-button> -->
                    </template>
                </pure-table>
            </template>
        </PureTableBar>
    </div>
</template>

<script lang='ts' setup>
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useDept } from "./utils/hook";
import { ref, nextTick, watch, onMounted, reactive } from 'vue'
import { useTranslationLang } from "@/layout/hooks/useTranslationLang";
const { locale } = useTranslationLang();
import { useI18n } from "vue-i18n";
const { t } = useI18n();

// iconify-icons
import AddFill from "@iconify-icons/ri/add-circle-line";
import EditPen from "@iconify-icons/ep/edit-pen";
import outlineDelete from '@iconify-icons/ic/outline-delete';
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import outboxIcon from '@iconify-icons/quill/outbox';
import Delete from '@iconify-icons/ep/delete';
import clearOutlined from '@iconify-icons/ant-design/clear-outlined';
import loadingIcon from '@iconify-icons/icon-park/loading';
import signOutBold from '@iconify-icons/ph/sign-out-bold';
import EpArrowDown from "@iconify-icons/ep/arrow-down-bold";
import Check from "@iconify-icons/ep/check";

const formRef = ref(null)

// const statusSelect = ref([
//     { value: 1, label: t('racklocations.idle') },
//     { value: 2, label: t('racklocations.disable') },
//     { value: 3, label: t('racklocations.locked') },
//     { value: 4, label: t('racklocations.cargoAlreadyLoaded') },
//     // { value: 5, label: '无货箱' },
// ])

const {
    columns,
    loading,
    dataList,
    openDialog,
    handleStatus,
    form,
    statusSelect,
    onSearch,
    resetForm,
    pagination,
    handleSizeChange,
    handleCurrentChange,
    attributeSelect,
    rackselect,
    categorySelect,
    addAppointOut,
    addDelteLoad,
    stationSelect,
    clearrackslocation,
    initialize,
    exportExcel,
    delLocation,
    agvModel,
    tableReloading
} = useDept();

const simplifiedColumns = ref(JSON.parse(localStorage.getItem('racklocationsColumns')) || columns.value.map(item => {
    return {
        id: item.id,
        isShow: true,
        label: item.label
    }
}))
const newColumns = ref(simplifiedColumns.value.filter(item => item.isShow).map(item => {
    return columns.value.find(item2 => item2.id === item.id)
}))
const tableHtml = ref(true);
const handleCheckboxChange = (isVisible) => {
    if (!isVisible) {
        newColumns.value = simplifiedColumns.value.filter(item => item.isShow).map(item => {
            return columns.value.find(item2 => item2.id === item.id)
        })
        tableHtml.value = false;
        nextTick(() => {
            tableHtml.value = true;
            localStorage.setItem('racklocationsColumns', JSON.stringify(simplifiedColumns.value));
        });
    }
}

watch(() => locale.value, (val) => {
    tableHtml.value = false
    nextTick(() => {
        const arrayIds = new Set(JSON.parse(localStorage.getItem('racklocationsColumns')).map(item => item.id));
        newColumns.value = tableReloading().filter(item => arrayIds.has(item.id));
        // localStorage.setItem('racklocationsColumns', JSON.stringify(tableReloading().map(item => {
        //     return {
        //         id: item.id,
        //         isShow: true,
        //         label: item.label
        //     }
        // })));
        // newColumns.value = tableReloading()
        tableHtml.value = true
    })
})

</script>

<style lang="scss" scoped>
.search-form {
    :deep(.el-form-item) {
        margin-bottom: 12px;
    }
}
</style>